package com.itzhanshen.easyexcel02;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WriteExcel {
    public static void main(String[] args) {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); // 居中
        //设置字体样式
        WriteFont font = new WriteFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)12);
        writeCellStyle.setWriteFont(font);
        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(writeCellStyle, writeCellStyle);

        // 生成excle报表
        EasyExcel.write("D:/报表.xls")
                .sheet("今日报表")
                .head(head())
                .registerWriteHandler(styleStrategy) // 样式
                .registerWriteHandler(new MyColumnStyle())
                .doWrite(data());
        System.out.println("写出成功");
    }
    public static List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        list.add(Arrays.asList("编号"));
        list.add(Arrays.asList("姓名"));
        list.add(Arrays.asList("年龄"));
        list.add(Arrays.asList("性别"));
        list.add(Arrays.asList("描述"));
        return list;
    }
    //组装表头信息
    private static List<List<String>> data() {
        List<List<String>> list = new ArrayList<List<String>>();
        list.add(Arrays.asList("1","风清扬","88","男","独孤九剑"));
        list.add(Arrays.asList("2","令狐冲","28","男","易筋经"));
        list.add(Arrays.asList("3","任盈盈","25","女","魔教圣姑"));
        list.add(Arrays.asList("4","任我行","60","男","吸星大法"));
        list.add(Arrays.asList("5","东方不败","48","男/女","葵花宝典"));
        return list;
    }
}
